#!/bin/bash

# 百联仓库物流管理系统 - 正式环境启动脚本 (简化版)
# 版本: 3.9.0

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 日志函数
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

echo "=================================="
echo "百联仓库物流管理系统 - 正式环境启动脚本"
echo "版本: 3.9.0"
echo "=================================="

# 检查Java环境
log_info "检查Java环境..."
if ! command -v java &> /dev/null; then
    log_error "未检测到Java环境，请先安装Java 8+"
    exit 1
fi
JAVA_VERSION=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2)
log_success "检测到Java版本: $JAVA_VERSION"

# 检查Maven环境
log_info "检查Maven环境..."
if ! command -v mvn &> /dev/null; then
    log_error "未检测到Maven环境，请先安装Maven"
    exit 1
fi
log_success "Maven环境检查通过"

# 启动MySQL服务
log_info "检查MySQL服务..."
if command -v mysql &> /dev/null; then
    if ! pgrep -x "mysqld" > /dev/null; then
        log_warning "MySQL服务未运行，尝试启动..."
        if command -v systemctl &> /dev/null; then
            sudo systemctl start mysql 2>/dev/null || sudo systemctl start mysqld 2>/dev/null
        elif command -v service &> /dev/null; then
            sudo service mysql start 2>/dev/null || sudo service mysqld start 2>/dev/null
        fi
        sleep 3
    fi
    if pgrep -x "mysqld" > /dev/null; then
        log_success "MySQL服务运行中"
    else
        log_warning "MySQL服务启动失败，请手动启动"
    fi
else
    log_warning "未检测到MySQL，请确保MySQL已安装并运行"
fi

# 启动Redis服务
log_info "检查Redis服务..."
if command -v redis-server &> /dev/null; then
    if ! pgrep -x "redis-server" > /dev/null; then
        log_warning "Redis服务未运行，尝试启动..."
        if command -v systemctl &> /dev/null; then
            sudo systemctl start redis 2>/dev/null
        elif command -v service &> /dev/null; then
            sudo service redis start 2>/dev/null
        else
            nohup redis-server > /dev/null 2>&1 &
        fi
        sleep 2
    fi
    if pgrep -x "redis-server" > /dev/null; then
        log_success "Redis服务运行中"
    else
        log_warning "Redis服务启动失败，请手动启动"
    fi
else
    log_warning "未检测到Redis，请确保Redis已安装并运行"
fi

# 启动Nginx服务
log_info "检查Nginx服务..."
if command -v nginx &> /dev/null; then
    if ! pgrep -x "nginx" > /dev/null; then
        log_warning "Nginx服务未运行，尝试启动..."
        if command -v systemctl &> /dev/null; then
            sudo systemctl start nginx 2>/dev/null
        elif command -v service &> /dev/null; then
            sudo service nginx start 2>/dev/null
        else
            sudo nginx 2>/dev/null
        fi
        sleep 2
    fi
    if pgrep -x "nginx" > /dev/null; then
        log_success "Nginx服务运行中"
    else
        log_warning "Nginx服务启动失败，请手动启动"
    fi
else
    log_warning "未检测到Nginx，跳过Nginx启动"
fi

# 创建必要目录
log_info "创建必要目录..."
mkdir -p logs uploadPath temp backup

# 编译项目
log_info "开始编译项目..."
if mvn clean package -DskipTests -q; then
    log_success "项目编译成功"
else
    log_error "项目编译失败"
    exit 1
fi

# 启动百联系统
log_info "启动百联系统..."
JAR_FILE="bailian-admin/target/bailian-admin.jar"
if [[ ! -f "$JAR_FILE" ]]; then
    log_error "未找到JAR文件: $JAR_FILE"
    exit 1
fi

# 设置生产环境配置
export SPRING_PROFILES_ACTIVE=prod

# 启动应用
nohup java -Xms512m -Xmx1024m \
    -Dspring.profiles.active=prod \
    -Dfile.encoding=UTF-8 \
    -Duser.timezone=Asia/Shanghai \
    -jar "$JAR_FILE" > logs/bailian.log 2>&1 &

PID=$!
log_success "百联系统启动中，进程ID: $PID"

# 等待应用启动
log_info "等待应用启动..."
for i in {1..30}; do
    if curl -s http://localhost:8080/actuator/health > /dev/null 2>&1; then
        log_success "百联系统启动成功！"
        log_info "访问地址: http://localhost:8080"
        log_info "日志文件: logs/bailian.log"
        break
    fi
    sleep 2
    echo -n "."
done

echo ""
echo "=================================="
log_success "启动脚本执行完成！"
echo "==================================" 